/**
 * @file STL_SafeQueue.h
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 安全队列
 * @version 0.0.1
 * @date 2020-07-16
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */
#ifndef __STL_SAFE_QUEUE_H__
#define __STL_SAFE_QUEUE_H__

#include <queue>
#include <mutex>

namespace STL
{

    template <typename T>
    class STL_SafeQueue
    {
    public:
        STL_SafeQueue() = default;

        ~STL_SafeQueue() = default;

        void push(const T &in)
        {
            std::lock_guard<std::mutex> g(m_mutex);
            m_queue.push(in);
        }

        bool empty()
        {
            return m_queue.empty();
        }

        size_t size()
        {
            return m_queue.size();
        }

        bool pop(T &out)
        {
            std::lock_guard<std::mutex> g(m_mutex);
            if (m_queue.empty())
            {
                return false;
            }
            out = m_queue.front();
            m_queue.pop();
            return true;
        }

        bool pop(){
            std::lock_guard<std::mutex> g(m_mutex);
            if (m_queue.empty())
            {
                return false;
            }
            m_queue.pop();
            return true;
        }

    private:
        std::queue<T> m_queue;
        std::mutex m_mutex;
    };

} // namespace STL

#endif //__STL_SAFE_QUEUE_H__